package MyStack;

import java.util.Arrays;

public class MyStack {
    public int[] elem;
    public int usedSize;
    public static int DEFULT_SIZE = 10;
    
    public MyStack() {
        this.elem = new int[DEFULT_SIZE];
    }
    
    public void push(int val) {
        if (isFull()) {
            //扩容
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
        elem[usedSize++] = val;
    }
    
    public int pop() {
        if (isEmpety()) {
            //栈为空，无法出栈
            throw new EmptyStackException("栈为空，无法弹出");
        }
        int popNumber = elem[usedSize-1];
        usedSize--;
        return popNumber;
    }
    
    public int peek() {
        if (isEmpety()) {
            //栈为空，无法出栈
            throw new EmptyStackException("栈为空，无法弹出");
        }
        return elem[usedSize - 1];
    }
    
    public boolean isFull() {
        return usedSize == elem.length;
    }
    
    public boolean isEmpety() {
        return usedSize == 0;
    }

}
